home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  3.7 KB  |  160 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     save.c
  4.  
  5.     This module handles saving article and message windows.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14.  
  15. #include "dlgutil.h"
  16. #include "glob.h"
  17. #include "save.h"
  18. #include "util.h"
  19. #include "sfutil.h"
  20.  
  21.  
  22.  
  23. /*    This routine removes colons and returns from the titles of
  24.     articles.  This insures that the string returned is a legal filename.
  25.     It also removes any leading check mark.
  26. */
  27.  
  28. static void TitleFilter (StringPtr title)
  29. {
  30.     short i;
  31.     
  32.     if (*title > 0 && title[1] == (unsigned char)'√') {
  33.         BlockMove(title+2, title+1, *title-1);
  34.         (*title)--;
  35.     }
  36.     
  37.     for (i=1; i<=*title; i++) {
  38.         if (title[i] == CR || title[i] == ':') title[i] = ' ';
  39.     }
  40.     if (*title > 31) *title = 31;
  41. }
  42.  
  43.  
  44. /*    GetFullMessageText gets the full text for a message window, 
  45.     including the header.
  46. */
  47.  
  48. void GetFullMessageText (WindowPtr wind, Handle *fullText, 
  49.     Boolean *mustDispose)
  50. {
  51.     TWindow **info;
  52.     TEHandle theTE;
  53.     Handle text, headerText;
  54.     long textLength, headerLength;
  55.     
  56.     info = (TWindow**)GetWRefCon(wind);
  57.     theTE = (**info).theTE;
  58.     text = (Handle)TEGetText(theTE);
  59.     if ((**info).headerShown) {
  60.         *fullText = text;
  61.         *mustDispose = false;
  62.     } else {
  63.         headerText = (**info).headerText;
  64.         headerLength = GetHandleSize(headerText);
  65.         textLength = GetHandleSize(text);
  66.         *fullText = MyNewHandle(headerLength + textLength);
  67.         BlockMove(*headerText, **fullText, headerLength);
  68.         BlockMove(*text, **fullText+headerLength, textLength);
  69.         *mustDispose = true;
  70.     }
  71. }
  72.  
  73.  
  74. /*    DoSaveWindow is called when the user issues a Save command for an
  75.     article or message window.
  76.     
  77.     Exit:    function result = true if file saved, false if canceled
  78.                 or error.
  79. */
  80.  
  81. Boolean DoSaveWindow (WindowPtr wind)
  82. {
  83.     StandardFileReply reply;
  84.     Str255 defaultName;
  85.     TWindow **info;
  86.     Handle text = nil;
  87.     OSErr err;
  88.     long length;
  89.     short fRefNum = 0, vRefNum;
  90.     EWindowKind kind;
  91.     Boolean isArticle, mustDispose=false;
  92.     CStr255 msg;
  93.     
  94.     GetWTitle(wind,defaultName);
  95.     TitleFilter(defaultName);
  96.     
  97.     info = (TWindow**)GetWRefCon(wind);
  98.     kind = (**info).kind;
  99.     isArticle = kind == kArticle || kind == kMiscArticle;
  100.  
  101.     /* Force Standard File to start in the right place */
  102.     
  103.     if (gPrefs.textDefaultDir) {
  104.         if (VolNameToVRefNum(gPrefs.textVolName, &vRefNum) == noErr) {
  105.             SFSaveDisk = -vRefNum;
  106.             CurDirStore = gPrefs.textDirID;
  107.         }
  108.     }
  109.     
  110.     if (isArticle) {
  111.         MyStandardPutFile("\pSave article as:", defaultName, &reply);
  112.     } else {
  113.         MyStandardPutFile("\pSave message as:", defaultName, &reply);
  114.     }
  115.     if (!reply.sfGood) return false;
  116.     
  117.     err = FSpOpenDF(&reply.sfFile, fsRdWrPerm, &fRefNum);
  118.     if (err == noErr) {
  119.         err = SetEOF(fRefNum, 0);
  120.         if (err != noErr) goto exit1;
  121.     } else if (err = fnfErr) {
  122.         err = FSpCreate(&reply.sfFile, gPrefs.textCreator, 'TEXT', reply.sfScript);
  123.         if (err != noErr) goto exit1;
  124.         err = FSpOpenDF(&reply.sfFile, fsRdWrPerm, &fRefNum);
  125.         if (err != noErr) goto exit1;
  126.     } else {
  127.         goto exit1;
  128.     }
  129.     
  130.     if (isArticle) {
  131.         text = (**info).fullText;
  132.     } else {
  133.         GetFullMessageText(wind, &text, &mustDispose);
  134.     }
  135.     length = GetHandleSize(text);
  136.     
  137.     HLock(text);
  138.     err = FSWrite(fRefNum,&length,*text);
  139.     if (err != noErr) goto exit1;
  140.     if (*(*text + length - 1) != CR) {
  141.         length = 1;
  142.         err = FSWrite(fRefNum, &length, CRSTR);
  143.         if (err != noErr) goto exit1;
  144.     }
  145.     HUnlock(text);
  146.     if (mustDispose) MyDisposHandle(text);
  147.     FSClose(fRefNum);
  148.     FlushVol(NULL, reply.sfFile.vRefNum);
  149.     return true;
  150.     
  151. exit1:
  152.     sprintf(msg, "Unexpected error %d while trying to save file “%#s”", 
  153.         err, reply.sfFile.name);
  154.     ErrorMessage(msg);
  155.     if (text != nil) HUnlock(text);
  156.     MyDisposHandle(text);
  157.     if (fRefNum != 0) FSClose(fRefNum);
  158.     return false;
  159. }
  160.